//    Openbravo POS is a point of sales application designed for touch screens.

package com.openbravo.pos.forms;

import cn.ekuma.epos.businesslogic.RoleUtil;

import com.openbravo.bean.infobean.UserInfo;
import com.openbravo.pos.base.*;

import cn.ekuma.epos.datalogic.I_DataLogicCustomers;
import cn.ekuma.epos.datalogic.I_DataLogicSystem;
import cn.ekuma.epos.datalogic.define.DataLogicSystemHelper;
import cn.ekuma.epos.db.table.I_Product;
import cn.ekuma.epos.panel.JFinderDialog;
import cn.ekuma.rabbit.ModelManager;
import cn.ekuma.security.bean.I_Role;
import com.openbravo.bean.AppUser;
import com.openbravo.bean.AppUserState;
import com.openbravo.bean.CloseCash;
import com.openbravo.bean.Location;
import com.openbravo.bean.infobean.CustomerInfo;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import java.util.Properties;
import java.util.UUID;
import javax.swing.*;
import net.xeoh.plugins.base.PluginManager;
import com.openbravo.pos.model.BaseSwingModel;
import com.openbravo.pos.printer.*;
import com.openbravo.pos.quickfinder.JProductFinder;
import com.openbravo.beans.*;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.loader.ImageUtils;
import com.openbravo.data.loader.query.QBFParameter;
import com.openbravo.format.Formats;
import com.openbravo.pos.bean.AppUserExt;
import com.openbravo.pos.scale.DeviceScale;
import com.openbravo.pos.scanpal2.Scanner;
import com.openbravo.pos.util.Hashcypher;
import com.openbravo.pos.util.ThumbNailBuilder;

/**JPanel
 *
 * @author adrianromero
 */
public  abstract class JRootApp extends JPanel implements AppView {

    /**
     * 
	 */
	private static final long serialVersionUID = 8898267365483230668L;
    protected AppProperties m_props;

    protected Properties m_propsdb = null;
    /**
    private String m_sActiveCashIndex;
    private int m_iActiveCashSequence;
    private Date m_dActiveCashDateStart;
    private Date m_dActiveCashDateEnd;
    **/
    protected String m_sInventoryLocation;

    protected StringBuffer inputtext;

    protected DeviceScale m_Scale;
    protected Scanner m_Scanner;
    protected DeviceTicket m_TP;
    protected TicketParser m_TTP;

    protected JPrincipalApp m_principalapp = null;

    protected AppUserState userState;

    protected CustomerInfo loginCustomer;

    protected boolean isRemoteModel=false;

    protected String sWareHouse;

    protected Location defLocation;

    protected int delayHouse;

    protected ModelManager modeManager;
    protected BaseSwingModel swingModel;

    protected AbstractPosSwingApp swingApp;
    protected I_DataLogicSystem m_dlSystem;
    protected I_DataLogicCustomers m_dlCustomer;
    protected PluginManager pmf;

    /** Creates new form JKeyBoardRootApp */
    public JRootApp() {
        // Inicializo los componentes visuales

        initComponents ();
        jScrollPane1.getVerticalScrollBar().setPreferredSize(new Dimension(35, 35));
    }
    private void escapeKeyProc() {
              tryToClose();
    }

    public abstract void enterKeyProc();

    protected void upKeyProc() {
         if(jTextField1.isFocusOwner())
             m_txtKeys.requestFocus();
         else if(jPasswordField1.isFocusOwner())
              jTextField1.requestFocus();
             else if(m_txtKeys.isFocusOwner())
                 jPasswordField1.requestFocus();
                 else
                     jTextField1.requestFocus();

    }

    protected void downKeyProc() {
         if(jTextField1.isFocusOwner())
             jPasswordField1.requestFocus();
         else if(jPasswordField1.isFocusOwner())
              m_txtKeys.requestFocus();
             else if(m_txtKeys.isFocusOwner())
                 jTextField1.requestFocus();
                 else
                     jTextField1.requestFocus();
    }

    public abstract boolean initApp(AppProperties props, AbstractSwingAppFactory swingAppFactory);
    

    public void tryToClose() {

        if (closeAppView()) {

            // success. continue with the shut down

            // apago el visor
            m_TP.getDeviceDisplay().clearVisor();
            // me desconecto de la base de datos.
            swingApp.stop();

            // Download Root form
            SwingUtilities.getWindowAncestor(this).dispose();
        }
    }

    // Interfaz de aplicacion
    public DeviceTicket getDeviceTicket(){
        return m_TP;
    }

    public DeviceScale getDeviceScale() {
        return m_Scale;
    }
    public Scanner getDeviceScanner() {
        return m_Scanner;
    }

    public String getInventoryLocation() {
        return m_sInventoryLocation;
    }
    /**
    public String getActiveCashIndex() {
        return m_sActiveCashIndex;
    }
    public int getActiveCashSequence() {
        return m_iActiveCashSequence;
    }
    public Date getActiveCashDateStart() {
        return m_dActiveCashDateStart;
    }
    public Date getActiveCashDateEnd(){
        return m_dActiveCashDateEnd;
    }
    public void setActiveCash(String sIndex, int iSeq, Date dStart, Date dEnd) {
        m_sActiveCashIndex = sIndex;
        m_iActiveCashSequence = iSeq;
        m_dActiveCashDateStart = dStart;
        m_dActiveCashDateEnd = dEnd;

        m_propsdb.setProperty("activecash", m_sActiveCashIndex);
        DataLogicSystemHelper.setResourceAsProperties(m_dlSystem,m_props.getHost() + "/properties", m_propsdb);
    }*/

    public AppProperties getProperties() {
        return m_props;
    }


    public void waitCursorBegin() {
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    }

    public void waitCursorEnd(){
        setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }

    public AppUserView getAppUserView() {
        return m_principalapp;
    }


    protected void listPeople() {
         
        try {

            jScrollPane1.getViewport().setView(null);

            JFlowPanel jPeople = new JFlowPanel();
            jPeople.applyComponentOrientation(getComponentOrientation());

            java.util.List people = m_dlSystem.listPeopleVisible();

            for (int i = 0; i < people.size(); i++) {

                AppUserExt user = (AppUserExt) people.get(i);

                JButton btn = new JButton(new AppUserAction(user));
                btn.applyComponentOrientation(getComponentOrientation());
                btn.setFocusPainted(false);
                btn.setFocusable(false);
                btn.setRequestFocusEnabled(false);
                btn.setHorizontalAlignment(SwingConstants.LEADING);
                btn.setMaximumSize(new Dimension(150, 50));
                btn.setPreferredSize(new Dimension(150, 50));
                btn.setMinimumSize(new Dimension(150, 50));

                jPeople.add(btn);
            }
            jScrollPane1.getViewport().setView(jPeople);
            jScrollPane1.setVisible(true);
        } catch (BasicException ee) {
            ee.printStackTrace();
        }
    }

    @Override
    public String getSystemCashIndex() {
        String sSystemCashIndex=null;
         // 设置系统现账,用于单据操作
        try {
        	CloseCash valcash =  m_dlSystem.findSystemCash("systemcash");
            if (valcash == null ) {
                // no la encuentro o no es de mi host por tanto creo una...
                sSystemCashIndex=UUID.randomUUID().toString();
                
                CloseCash newCash=new CloseCash();
                newCash.setMoneyID(sSystemCashIndex);
                newCash.setHost(m_props.getHost());
                newCash.setHostSequence(m_dlSystem.getSequenceCash(m_props.getHost()) + 1);
                newCash.setDataStart(new Date());
                newCash.setDataEnd(null);
                newCash.setName("systemcash");
                m_dlSystem.insert(newCash);
                // creamos la caja activa
                //m_dlSystem.execInsertCash(new Object[] {sSystemCashIndex, m_props.getHost(), m_dlSystem.getSequenceCash(m_props.getHost()) + 1, new Date(), null,"systemcash",new Date()});
            }else{
                sSystemCashIndex=(String) valcash.getMoneyID();
            }
        } catch (BasicException e) {
            // Casco. Sin caja no hay pos
            MessageInf msg = new MessageInf(MessageInf.SGN_NOTICE, AppLocal.getIntString("message.cannotclosecash"), e);
            msg.show(this);
        }
        return sSystemCashIndex;
    }

    public String getUserCashIndex(){
        return m_propsdb.getProperty(getAppUserView().getUser().getId());
    }

    @Override
    public void setUserCashIndex(String value) {
        String userID=getAppUserView().getUser().getId();
        m_propsdb.setProperty(userID, value);
        DataLogicSystemHelper.setResourceAsProperties(m_dlSystem,m_props.getHost() + "/properties", m_propsdb);
    }

    public void checkUserCashIndex(){
            //设置员工账
            String userID =getAppUserView().getUser().getId();
            try{
            String sUserCashIndex = m_propsdb.getProperty(userID);
            CloseCash valcash = sUserCashIndex == null
                        ? null
                        : m_dlSystem.findPeopleActiveCash(sUserCashIndex,delayHouse);
            if (valcash == null) {
                    // no la encuentro o no es de mi host por tanto creo una...
                    sUserCashIndex = UUID.randomUUID().toString();
                    valcash=new CloseCash();
                    valcash.setMoneyID(sUserCashIndex);
                    valcash.setHost(m_props.getHost());
                    valcash.setHostSequence(m_dlSystem.getSequenceCash(m_props.getHost()) + 1);
                    valcash.setDataStart(new Date());
                    valcash.setDataEnd(null);
                    valcash.setName(getAppUserView().getUser().getName());
                    m_dlSystem.insert(valcash);
                    
                    // creamos la caja activa
                    //m_dlSystem.execInsertCash(new Object[]{sUserCashIndex, m_props.getHost(), m_dlSystem.getSequenceCash(m_props.getHost()) + 1, new Date(), null,getAppUserView().getUser().getUserInfo().getName(),new Date()});
                    setUserCashIndex(sUserCashIndex);
                    
                    System.out.println("add userCashIndex:"+sUserCashIndex);
           }
          System.out.println("prop.getHost:"+m_props.getHost()+"/t dbCash is:"+valcash.getHost()+"/t is equ:"+m_props.getHost().equals(valcash.getHost()));
         
         } catch (BasicException ex) {
            // Casco. Sin caja no hay pos
            MessageInf msg = new MessageInf(MessageInf.SGN_NOTICE, AppLocal.getIntString("message.cannotclosecash"), ex);
            msg.show(this);
        }
    }

    protected final ThumbNailBuilder tnb = new ThumbNailBuilder(32, 32, "com/openbravo/images/yast_sysadmin.png");
    protected String menuModel;

    // La accion del selector
    protected class AppUserAction extends AbstractAction {

        private AppUserExt m_actionuser;
        Icon icon;
        
        public AppUserAction(AppUserExt user) {
            m_actionuser = user;
            icon=new ImageIcon(tnb.getThumbNail(ImageUtils.readImage(user.getM_Image())));
            putValue(Action.SMALL_ICON,icon);
            putValue(Action.NAME, m_actionuser.getName());
        }

        public AppUser getUser() {
            return m_actionuser;
        }

        public void actionPerformed(ActionEvent evt) {
            // String sPassword = m_actionuser.getPassword();
            if (RoleUtil.authenticate(m_actionuser)) {
                // p'adentro directo, no tiene password
                openAppView(m_actionuser);
            } else {
                // comprobemos la clave antes de entrar...
                String sPassword = JPasswordDialog.showEditPassword(JRootApp.this,
                        AppLocal.getIntString("Label.Password"),
                        m_actionuser.getName(),
                        icon);
                if (sPassword != null) {
                    if (Hashcypher.authenticate(sPassword, m_actionuser.getPassword())) {
                        openAppView(m_actionuser);
                    } else {
                        MessageInf msg = new MessageInf(MessageInf.SGN_WARNING, AppLocal.getIntString("message.BadPassword"));
                        msg.show(JRootApp.this);
                    }
                }
            }
        }
    }

    protected void showView(String view) {
        CardLayout cl = (CardLayout)(m_jPanelContainer.getLayout());
        cl.show(m_jPanelContainer, view);
    }

    protected void openAppView(final AppUser user) {
        if (closeAppView()) {
        	if(!isCustomerView()){
        	try {
        		userState=m_dlSystem.getAppUserState(user.getId());
				if(userState!=null){
					String loginState=Formats.TIMESTAMP.formatValue(userState.getLoginTime())+"<"+userState.getLoginAddr()+">";
					int res = JOptionPane.showConfirmDialog(this, loginState+AppLocal.getIntString("message.loginStateCheckFalse"), AppLocal.getIntString("title.editor"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
			        if (res == JOptionPane.NO_OPTION){
			        	return;
			        }else{
			        	m_dlSystem.clearUserRecord(user.getId());
			        	m_dlSystem.deleteAppUserState(userState.getKey());
			        }
				}
				userState=new AppUserState();
				userState.setUserID(user.getId());
				userState.setLoginTime(new Date());
				userState.setLoginAddr(m_props.getHost());
				m_dlSystem.insert(userState);
			} catch (BasicException e) {
				MessageInf msg = new MessageInf(MessageInf.SGN_WARNING, e.getMessage());
                msg.show(JRootApp.this);
			}
        	}
        	try{
        	modeManager.setLoginRole(new I_Role(){
				@Override
				public String getID() {
					return user.getRole();
				}

				@Override
				public String getName() {
					return null;
				}
        	});}catch (BasicException e){
                MessageInf msg = new MessageInf(MessageInf.SGN_WARNING, e.getMessage());
                msg.show(JRootApp.this);
            }
            m_principalapp = new JPrincipalApp(this, user,swingModel);
            // The user status notificator
            jPanel3.add(m_principalapp.getNotificator());
            jPanel3.revalidate();
            // The main panel
            m_jPanelContainer.add(m_principalapp, "_" + m_principalapp.getUser().getId());
            showView("_" + m_principalapp.getUser().getId());
            if(m_props.getProperty(AppConfig.APP_LOGERABLE).equalsIgnoreCase("true")){
            	swingApp.initLoger(user.getName(), m_props.getHost());
            	
            }
            
            m_principalapp.activate();
        }
    }

    public int getMaxsequenceCash() throws BasicException{
        return m_dlSystem.getSequenceCash(m_props.getHost());
    }

	public boolean closeAppView() {

		if (m_principalapp == null) {
			return true;
		} else if (!m_principalapp.deactivate()) {
			return false;
		} else {
			if(!isCustomerView()){
				AppUserState temp=null;
				try {
					temp = m_dlSystem.getAppUserState(m_principalapp.getUser().getId());
				} catch (BasicException e) {
					MessageInf msg = new MessageInf(MessageInf.SGN_WARNING,
							e.getMessage());
					msg.show(JRootApp.this);
				}
				if (temp != null) {
						if ( Formats.TIMESTAMP
								.formatValue(temp.getLoginTime()).equalsIgnoreCase(Formats.TIMESTAMP.formatValue(userState
											.getLoginTime())))
							try {
								m_dlSystem.deleteAppUserState(userState.getKey());
							} catch (BasicException e) {
								MessageInf msg = new MessageInf(MessageInf.SGN_WARNING,
										e.getMessage());
								msg.show(JRootApp.this);
							}
						else {
							String loginState = Formats.TIMESTAMP
									.formatValue(temp.getLoginTime())
									+ "<"
									+ temp.getLoginAddr()
									+ ">"
									+ "--"
									+ Formats.TIMESTAMP.formatValue(userState
											.getLoginTime())
									+ "<"
									+ userState.getLoginAddr() + ">";
							MessageInf msg = new MessageInf(
									MessageInf.SGN_WARNING,
									loginState
											+ AppLocal
													.getIntString("message.loginStateCheckFalse"));
							msg.show(JRootApp.this);
						}
					}
			
			}

			// the status label
			jPanel3.remove(m_principalapp.getNotificator());
			jPanel3.revalidate();
			jPanel3.repaint();

			// remove the card
			m_jPanelContainer.remove(m_principalapp);
			m_principalapp = null;

			showLogin();

			return true;
		}
	}

    protected abstract void showLogin();

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the FormEditor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        m_jPanelTitle = new javax.swing.JPanel();
        m_jLblTitle = new javax.swing.JLabel();
        poweredby = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        m_jPanelContainer = new javax.swing.JPanel();
        m_jPanelLogin = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        m_jLogonName = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel2 = new javax.swing.JPanel();
        jPanel8 = new javax.swing.JPanel();
        m_jClose = new javax.swing.JButton();
        m_txtKeys = new javax.swing.JPasswordField();
        m_jPanelDown = new javax.swing.JPanel();
        panelTask = new javax.swing.JPanel();
        m_jHost = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(1024, 768));
        setLayout(new java.awt.BorderLayout());

        m_jPanelTitle.setBorder(javax.swing.BorderFactory.createMatteBorder(0, 0, 1, 0, javax.swing.UIManager.getDefaults().getColor("Button.darkShadow")));
        m_jPanelTitle.setLayout(new java.awt.BorderLayout());

        m_jLblTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        m_jLblTitle.setText("Window.Title");
        m_jPanelTitle.add(m_jLblTitle, java.awt.BorderLayout.CENTER);

        poweredby.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/poweredby.png"))); // NOI18N
        poweredby.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 5, 0, 5));
        m_jPanelTitle.add(poweredby, java.awt.BorderLayout.LINE_END);

        jLabel2.setPreferredSize(new java.awt.Dimension(142, 34));
        m_jPanelTitle.add(jLabel2, java.awt.BorderLayout.LINE_START);

        add(m_jPanelTitle, java.awt.BorderLayout.NORTH);

        m_jPanelContainer.setLayout(new java.awt.CardLayout());

        m_jPanelLogin.setLayout(new java.awt.BorderLayout());

        jPanel4.setLayout(new javax.swing.BoxLayout(jPanel4, javax.swing.BoxLayout.Y_AXIS));

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/logo.png"))); // NOI18N
        jLabel1.setText("<html><center>Openbravo POS is a point of sale application designed for touch screens.<br>" +
            "Copyright \u00A9 2007-2009 Openbravo, S.L.<br>" +
            "http://www.openbravo.com/product/pos<br>" +
            "<br>" +
            "Openbravo POS is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.<br>" +
            "<br>" +
            "Openbravo POS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.<br>" +
            "<br>" +
            "You should have received a copy of the GNU General Public License along with Openbravo POS.  If not, see http://www.gnu.org/licenses/.<br>" +
            "</center>");
        jLabel1.setAlignmentX(0.5F);
        jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jLabel1.setMaximumSize(new java.awt.Dimension(800, 1024));
        jLabel1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jPanel4.add(jLabel1);

        m_jPanelLogin.add(jPanel4, java.awt.BorderLayout.CENTER);

        m_jLogonName.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        m_jLogonName.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setPreferredSize(new java.awt.Dimension(450, 118));
        m_jLogonName.add(jScrollPane1);

        jPanel2.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 5, 0, 5));
        jPanel2.setPreferredSize(new Dimension(300, 118));
        jPanel8.setLayout(new java.awt.GridLayout(2, 2, 3, 3));

        m_txtKeys.setPreferredSize(new java.awt.Dimension(115,20));
        /**
        m_txtKeys.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                m_txtKeysKeyTyped(evt);
            }
        });
        **/
        jPanel8.add(m_txtKeys);

        m_jClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/exit.png"))); // NOI18N
        m_jClose.setText(AppLocal.getIntString("Button.Close")); // NOI18N
        m_jClose.setFocusPainted(false);
        m_jClose.setFocusable(false);
        m_jClose.setPreferredSize(new java.awt.Dimension(115, 35));
        m_jClose.setRequestFocusEnabled(false);
        m_jClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_jCloseActionPerformed(evt);
            }
        });
        jPanel2.setLayout(new BoxLayout(jPanel2, BoxLayout.Y_AXIS));
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jPasswordField1 = new javax.swing.JPasswordField();
        
                jPanel1.setLayout(new java.awt.GridLayout(2, 2, 2, 2));
                
                        jLabel3.setText(AppLocal.getIntString("Label.DbUser"));
                        jLabel3.setMaximumSize(new java.awt.Dimension(40, 20));
                        jPanel1.add(jLabel3);
                        
                                jTextField1.setText("0001");
                                jTextField1.setPreferredSize(new java.awt.Dimension(90,20));
                                jPanel1.add(jTextField1);
                                
                                        jLabel4.setText(AppLocal.getIntString("Label.DbPassword"));
                                        jLabel4.setMaximumSize(new java.awt.Dimension(40, 20));
                                        jPanel1.add(jLabel4);
                                        
                                                jPasswordField1.setText("111");
                                                jPasswordField1.setPreferredSize(new java.awt.Dimension(90,20));
                                                jPanel1.add(jPasswordField1);
                                                
                                                        jPanel2.add(jPanel1);
        jPanel8.add(m_jClose);

        jPanel2.add(jPanel8);

        m_jLogonName.add(jPanel2);

        jPanel5.add(m_jLogonName);

        m_jPanelLogin.add(jPanel5, java.awt.BorderLayout.SOUTH);

        m_jPanelContainer.add(m_jPanelLogin, "login");

        add(m_jPanelContainer, java.awt.BorderLayout.CENTER);

        m_jPanelDown.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 0, 0, 0, javax.swing.UIManager.getDefaults().getColor("Button.darkShadow")));
        m_jPanelDown.setLayout(new java.awt.BorderLayout());

        m_jHost.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/display.png"))); // NOI18N
        m_jHost.setText("*Hostname");
        panelTask.add(m_jHost);

        m_jPanelDown.add(panelTask, java.awt.BorderLayout.LINE_START);

        KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        ActionListener actionListener = new ActionListener() {
           @Override
              public void actionPerformed(ActionEvent actionEvent) {
                 escapeKeyProc();
          }
        };
        m_jPanelLogin.registerKeyboardAction(actionListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);

        stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
        actionListener = new ActionListener() {
           @Override
              public void actionPerformed(ActionEvent actionEvent) {
                 enterKeyProc();
          }
        };
        m_jPanelLogin.registerKeyboardAction(actionListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);

        stroke = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
        actionListener = new ActionListener() {
           @Override
              public void actionPerformed(ActionEvent actionEvent) {
                 upKeyProc();
          }
        };
        m_jPanelLogin.registerKeyboardAction(actionListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);

        stroke = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);
        actionListener = new ActionListener() {
           @Override
              public void actionPerformed(ActionEvent actionEvent) {
                 downKeyProc();
          }
        };
        m_jPanelLogin.registerKeyboardAction(actionListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);

        add(m_jPanelDown, java.awt.BorderLayout.SOUTH);
        
        panel = new JPanel();
        m_jPanelDown.add(panel, BorderLayout.EAST);
        
        txtCommand = new JTextField();
        txtCommand.setHorizontalAlignment(SwingConstants.RIGHT);
        
        txtCommand.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	 QBFParameter  para=new QBFParameter(I_Product.CODE);
                 para.equal(txtCommand.getText());
                 JFinderDialog finderDialog=JFinderDialog.getInstance(JRootApp.this, new JProductFinder(swingModel.getDLERP()));
                 finderDialog.query(para);   
            }
        });
        
        panel.add(txtCommand);
        txtCommand.setColumns(20);
        jPanel3 = new javax.swing.JPanel();
        panel.add(jPanel3);
    }// </editor-fold>
    

    private void m_jCloseActionPerformed(java.awt.event.ActionEvent evt) {
        tryToClose();
    }

    // Variables declaration - do not modify
    protected javax.swing.JLabel jLabel1;
    protected javax.swing.JLabel jLabel2;
    protected javax.swing.JLabel jLabel3;
    protected javax.swing.JLabel jLabel4;
    protected javax.swing.JPanel jPanel1;
    protected javax.swing.JPanel jPanel2;
    protected javax.swing.JPanel jPanel3;
    protected javax.swing.JPanel jPanel4;
    protected javax.swing.JPanel jPanel5;
    protected javax.swing.JPanel jPanel8;
    protected javax.swing.JPasswordField jPasswordField1;
    protected javax.swing.JScrollPane jScrollPane1;
    protected javax.swing.JTextField jTextField1;
    protected javax.swing.JButton m_jClose;
    protected javax.swing.JLabel m_jHost;
    protected javax.swing.JLabel m_jLblTitle;
    protected javax.swing.JPanel m_jLogonName;
    protected javax.swing.JPanel m_jPanelContainer;
    protected javax.swing.JPanel m_jPanelDown;
    protected javax.swing.JPanel m_jPanelLogin;
    protected javax.swing.JPanel m_jPanelTitle;
    protected javax.swing.JPasswordField m_txtKeys;
    protected javax.swing.JPanel panelTask;
    protected javax.swing.JLabel poweredby;
    protected JPanel panel;
    protected JTextField txtCommand;
    // End of variables declaration
	@Override
	public boolean isCustomerView() {
		return loginCustomer!=null;
	}
	@Override
	public CustomerInfo getCustomerInfo() {
		return loginCustomer;
	}

	public String getsWareHouse() {
		return sWareHouse;
	}
	@Override
	public Location getDefineLocation() {
		return defLocation;
	}
	public ModelManager getModeManager() {
		return modeManager;
	}
	public void showCommandInput() {
		txtCommand.setVisible(true);
	}
	
	public void setMaxWindow(boolean flag){
		m_jPanelTitle.setVisible(!flag);
		m_jPanelDown.setVisible(!flag);
	}

    public abstract void initRolePermission(AppUser appuser);


    @Override
    public UserInfo getUserInfo() {
        AppUser user=m_principalapp.getUser();
        return new UserInfo(user.getId(),user.getName());
    }
}
